package titan.data.components;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import titan.messages.Messages;
import titan.utils.FileUtils;

/**
 * Class to load the data of a component
 */
public class ComponentData {
	private ArrayList<ComponentEngine> engines;
	private ArrayList<File> folders;
	private File ajax, css, function, js, _class;
	private String name, doc, destination;

	/**
	 * Read and instantiate component data and its engines from the given path
	 * @param path - the component path
	 * @throws Exception if there was an error while instantiating component data
	 * @see ComponentEngine#ComponentEngine(File)
	 */
	public ComponentData(File path) throws Exception{
		name = path.getName();
		folders = new ArrayList<File>();
		engines = new ArrayList<ComponentEngine>();
		File[] myFiles = path.listFiles();
		if(myFiles == null)
			throw new Exception(Messages.CompFolderNFound+path);
		Arrays.sort(myFiles, new Comparator<File>() { 
			public int compare(File file1, File file2) {
				return file1.compareTo(file2);
			}
        });
		for(int i = 0; i < myFiles.length; i++){
			if(myFiles[i].isDirectory()){
				if(myFiles[i].getName().compareTo("_doc") == 0){ //$NON-NLS-1$
					File[] docFiles = myFiles[i].listFiles();
					for(int j = 0; j < docFiles.length; j++)
						if(docFiles[j].getName().compareTo(Messages.language_txt) == 0){
							try{
								doc = FileUtils.readTextFile(docFiles[j]);
							} catch (Exception e) {
								throw new Exception(Messages.ErReadCompDoc+docFiles[j].getAbsolutePath());
							}
							break;
						}
				}
				folders.add(myFiles[i]);
			}
			else{
				if(myFiles[i].getName().compareTo("_ajax.php") == 0){ //$NON-NLS-1$
					ajax = myFiles[i];
				} else if(myFiles[i].getName().compareTo("_css.php") == 0){ //$NON-NLS-1$
					css = myFiles[i];
				} else if(myFiles[i].getName().compareTo("_function.php") == 0){ //$NON-NLS-1$
					function = myFiles[i];
				} else if(myFiles[i].getName().compareTo("_js.php") == 0){ //$NON-NLS-1$
					js = myFiles[i];
				} else if(myFiles[i].getName().compareTo("_class.php") == 0){ //$NON-NLS-1$
					_class = myFiles[i];
				} else{
					ComponentEngine ce = new ComponentEngine(myFiles[i]);
					if(i+1 < myFiles.length){
						String[] name1 = myFiles[i].getName().split("\\."); //$NON-NLS-1$
						String[] name2 = myFiles[i+1].getName().split("\\."); //$NON-NLS-1$
						if(name1[0].compareTo(name2[0]) == 0){
							ce.setFile(myFiles[++i]);
							if(i+1 < myFiles.length){
								name1 = name2;
								name2 = myFiles[i+1].getName().split("\\."); //$NON-NLS-1$
								if(name1[0].compareTo(name2[0]) == 0)
									ce.setFile(myFiles[++i]);
							}
						}
					}
					engines.add(ce);
				}
			}
		}
	}

	/**
	 * Copy component data (except component engines) from repository to the path given
	 * @param compPath - the destination path
	 * @throws Exception if it doesn't get to copy some file
	 */
	public void copy(String compPath) throws Exception {
		destination = compPath;
		if(!compPath.endsWith(File.separator))
			destination += File.separator;
		File dest = new File(destination);
		try {
			dest.mkdirs();
			for(int i = 0; i < folders.size(); i++){
				dest  = new File(destination+folders.get(i).getName());
				FileUtils.copyAllFiles(folders.get(i), dest);
			}
			if(ajax != null){
				dest  = new File(destination+ajax.getName());
				FileUtils.copyFile(ajax, dest);
			}
			if(css != null){
				dest  = new File(destination+css.getName());
				FileUtils.copyFile(css, dest);
			}
			if(function != null){
				dest  = new File(destination+function.getName());
				FileUtils.copyFile(function, dest);
			}
			if(js != null){
				dest  = new File(destination+js.getName());
				FileUtils.copyFile(js, dest);
			}
			if(_class != null){
				dest  = new File(destination+_class.getName());
				FileUtils.copyFile(_class, dest);
			}
		} catch(Exception e){
			throw new Exception(Messages.ErCopyComp+name+": "+e.getMessage()); //$NON-NLS-1$
		}
	}

	/**
	 * Copy the indexth engine to the path given at {@link ComponentData#copy(String) copy}
	 * @see ComponentEngine#copy(String)
	 * @param index - the engine index
	 * @throws Exception if {@link ComponentEngine#copy(String)} threw it
	 */
	public void copyEngine(int index) throws Exception{
		engines.get(index).copy(destination);
	}

	/**
	 * Get component name
	 * @return component name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Get number of engines
	 * @return number of engines
	 */
	public int getEnginesNumber(){
		return engines.size();
	}

	/**
	 * Get component documentation
	 * !//
	 * @return component documentation
	 */
	public String getDoc(){
		return doc;
	}

	/**
	 * Get name of engine i
	 * //?//:!//
	 * @param index engine index
	 * @return name of the engine
	 */
	public String getEngineName(int index){
		return engines.get(index).getName();
	}
}